//
//  MemberInfoViewController.m
//  Rail
//
//  Created by hychen on 2018/3/4.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "MemberInfoViewController.h"
#import "BMUsefulPickerView.h"
#import "PopoverView.h"

@interface MemberInfoViewController ()
@property (nonatomic, strong) NSArray<UserDeviceUsedModel *> *usedDeviceArray;
@property (nonatomic, assign) NSInteger tbkCount;
@end

@implementation MemberInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem addSimpleTitleView:@"人员信息"];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)czh_setGroup
{
    [super czh_setGroup];
    
    self.sectionGroups = [NSMutableArray array];
    BMWeakSelf
    
#warning 名称头像
    //名称头像
    CZHAvatartem *avatartem = [CZHAvatartem itemWithImageName:nil title:self.model.username?:@"" canClick:NO];
    avatartem.avater = self.model.logo_url;
    avatartem.hideArrow = YES;
    CZHSettingFrameModel *avatarFrameModel = [[CZHSettingFrameModel alloc] init];
    avatarFrameModel.cellHeight = 70;
    avatarFrameModel.bgColor = [UIColor whiteColor];
    avatarFrameModel.item = avatartem;
    
    //性别
//    CZHBadgeItem *sexItem = [CZHBadgeItem itemWithImageName:nil title:@"性别" canClick:YES haveLine:YES];
    CZHBadgeItem *sexItem = [CZHBadgeItem itemWithImageName:nil title:@"性别" canClick:NO haveLine:YES];
    sexItem.hideArrow = YES;
    sexItem.detail = [self.model.sex isEqualToString:@"1"]?@"男":@"女";
//    sexItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
//        [BMUsefulPickerView showSingleColPickerWithToolBarText:@"请选择性别" data:@[@"男",@"女"] defaultIndex:0 toolBarStyle:BMPickerViewToolBarStyleText cancelBlock:^{
//
//        } clickBlock:^(NSInteger selectedIndex, NSString *selectedValue) {
//
//        }];
//    };
    CZHSettingFrameModel *sexFrameModel = [[CZHSettingFrameModel alloc] init];
    sexFrameModel.cellHeight = 50;
    sexFrameModel.bgColor = [UIColor whiteColor];
    sexFrameModel.item = sexItem;;
    
    //手机号码
//    CZHEditArrowItem *phoneItem = [CZHEditArrowItem itemWithImageName:nil title:@"手机号码" canClick:YES] ;
    CZHBadgeItem *phoneItem = [CZHBadgeItem itemWithImageName:nil title:@"手机号码" canClick:NO];
    phoneItem.hideArrow = YES;
    phoneItem.detail = self.model.mobile;
//    phoneItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *phomeFrameModel = [[CZHSettingFrameModel alloc] init];
    phomeFrameModel.cellHeight = 50;
    phomeFrameModel.bgColor = [UIColor whiteColor];
    phomeFrameModel.item = phoneItem;
    
    //身份证号
//    CZHEditArrowItem *cardItem = [CZHEditArrowItem itemWithImageName:nil title:@"身份证号" canClick:YES] ;
    CZHBadgeItem *cardItem = [CZHBadgeItem itemWithImageName:nil title:@"身份证号" canClick:NO];
    cardItem.hideArrow = YES;
    cardItem.detail = self.model.cardno;
//    cardItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *cardFrameModel = [[CZHSettingFrameModel alloc] init];
    cardFrameModel.cellHeight = 50;
    cardFrameModel.bgColor = [UIColor whiteColor];
    cardFrameModel.item = cardItem;
    
    //出生日期
    CZHBadgeItem *birthdayItem = [CZHBadgeItem itemWithImageName:nil title:@"出生日期" canClick:NO haveLine:YES];
    birthdayItem.hideArrow = YES;
    birthdayItem.detail = self.model.birthday;
//    birthdayItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
//        BMDatePickerViewStyle *style = [BMDatePickerViewStyle new];
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//        NSDate *now = [NSDate date];
//        style.maximumDate = now;
//        [BMUsefulPickerView showDatePickerWithToolBarText:@"请选择出生日期" withStyle:style toolBarStyle:BMPickerViewToolBarStyleText cancelBlock:^{
//
//        } clickBlock:^(NSDate *selectedDate) {
//            //保存出生日期
//        } valueChangeBlock:^(NSDate *selectedDate) {
//
//        }];
//    };
    CZHSettingFrameModel *birthdayFrameModel = [[CZHSettingFrameModel alloc] init];
    birthdayFrameModel.cellHeight = 50;
    birthdayFrameModel.bgColor = [UIColor whiteColor];
    birthdayFrameModel.item = birthdayItem;;
    
    
    //电子邮箱
//    CZHEditArrowItem *mailItem = [CZHEditArrowItem itemWithImageName:nil title:@"电子邮箱" canClick:YES] ;
    CZHBadgeItem *mailItem = [CZHBadgeItem itemWithImageName:nil title:@"电子邮箱" canClick:NO];
    mailItem.hideArrow = YES;
    mailItem.detail = self.model.email;
//    mailItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *mailFrameModel = [[CZHSettingFrameModel alloc] init];
    mailFrameModel.cellHeight = 50;
    mailFrameModel.bgColor = [UIColor whiteColor];
    mailFrameModel.item = mailItem;
    
    //专业
//    CZHEditArrowItem *majorItem = [CZHEditArrowItem itemWithImageName:nil title:@"专业" canClick:YES] ;
    CZHBadgeItem *majorItem = [CZHBadgeItem itemWithImageName:nil title:@"专业" canClick:NO];
    majorItem.hideArrow = YES;
    majorItem.detail = self.model.specialty;
//    majorItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *majorFrameModel = [[CZHSettingFrameModel alloc] init];
    majorFrameModel.cellHeight = 50;
    majorFrameModel.bgColor = [UIColor whiteColor];
    majorFrameModel.item = majorItem;
    
    //学历
//    CZHEditArrowItem *educationItem = [CZHEditArrowItem itemWithImageName:nil title:@"学历" canClick:YES] ;
    CZHBadgeItem *educationItem = [CZHBadgeItem itemWithImageName:nil title:@"学历" canClick:NO];
    educationItem.hideArrow = YES;
    educationItem.detail = self.model.education;
//    educationItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *educationFrameModel = [[CZHSettingFrameModel alloc] init];
    educationFrameModel.cellHeight = 50;
    educationFrameModel.bgColor = [UIColor whiteColor];
    educationFrameModel.item = educationItem;
    
    //岗位
//    CZHEditArrowItem *positionItem = [CZHEditArrowItem itemWithImageName:nil title:@"岗位" canClick:YES] ;
    CZHBadgeItem *positionItem = [CZHBadgeItem itemWithImageName:nil title:@"岗位" canClick:NO];
    positionItem.hideArrow = YES;
    positionItem.detail = self.model.job;
//    positionItem.editCompleteBlock = ^(NSString *inputString) {//编辑完成回调
//        BMStrongSelf
//        //请求接口
//    };
    CZHSettingFrameModel *positionFrameModel = [[CZHSettingFrameModel alloc] init];
    positionFrameModel.cellHeight = 50;
    positionFrameModel.bgColor = [UIColor whiteColor];
    positionFrameModel.item = positionItem;
    
    NSMutableArray *items = [NSMutableArray arrayWithArray:@[avatarFrameModel,sexFrameModel,phomeFrameModel,cardFrameModel,birthdayFrameModel,mailFrameModel,majorFrameModel,educationFrameModel,positionFrameModel]];
    
    if (self.model.is_tbk) {
        CZHButtonItem *locateItem = [CZHButtonItem itemWithImageName:nil title:@"归还对讲设备"];
        locateItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
            [weakSelf requestData:@"1"];
        };
        CZHSettingFrameModel *returnLocateFrameModel = [[CZHSettingFrameModel alloc] init];
        returnLocateFrameModel.cellHeight = 80;
        returnLocateFrameModel.bgColor = [UIColor whiteColor];
        returnLocateFrameModel.item = locateItem;
        [items addObject:returnLocateFrameModel];
    }
    
    if (self.model.is_locate) {
        CZHButtonItem *tbkItem = [CZHButtonItem itemWithImageName:nil title:@"归还定位设备"];
        tbkItem.operationBlock = ^(CZHBaseSettingItem *item, NSIndexPath *indexPath) {
            [weakSelf requestData:@"2"];
        };
        CZHSettingFrameModel *returnTbkFrameModel = [[CZHSettingFrameModel alloc] init];
        returnTbkFrameModel.cellHeight = 80;
        returnTbkFrameModel.bgColor = [UIColor whiteColor];
        returnTbkFrameModel.item = tbkItem;
        [items addObject:returnTbkFrameModel];
    }
    
    CZHSectionItem *sectionZero = [[CZHSectionItem alloc] init];
    sectionZero.items = items;
    [self.sectionGroups addObject:sectionZero];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = [super tableView:tableView numberOfRowsInSection:section];
    //
    BOOL bLocate = NO;
    if (bLocate) {
        count++;
    }
    //
    BOOL bTbk = NO;
    if (bTbk) {
        count++;
    }
    return count;
}

#pragma mark - 归还设备
- (void)requestData:(NSString *)type {
    Show_NoMsg
    BMWeakSelf
    NSString *account = self.model.user_type == 0 ? self.model.account : @"";
    NSString *mobile = self.model.user_type == 0 ? @"" : self.model.mobile;
    NSString *areaid = self.model.areaid?:@"";
    [HttpRequest loadUserDeviceUsedListDataWithUserAccount:account mobile:mobile areaId:areaid success:^(NSArray *object) {
        weakSelf.usedDeviceArray = nil;
        NSMutableArray *tbkArray = [NSMutableArray array];
        NSMutableArray *locArray = [NSMutableArray array];
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            Dismiss_NoMsg
            for (UserDeviceUsedModel *model in object) {
                if ([model isKindOfClass:[UserDeviceUsedModel class]]) {
                    if ([model.typecode isEqualToString:@"VOIP-A"]) {
                        [tbkArray addObject:model];
                    }
                    else if ([model.typecode isEqualToString:@"GPS"]) {
                        [locArray addObject:model];
                    }
                }
            }
            if ([type isEqualToString:@"1"]) {
                //对讲
                weakSelf.usedDeviceArray = tbkArray;
            }
            else if ([type isEqualToString:@"2"]) {
                //定位
                weakSelf.usedDeviceArray = locArray;
            }
            [self showPopoverData];
        }
        else {
            Show_SV_Error_Msg(@"无设备可选用")
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_SV_Error_Msg(msg)
    }];
}

- (void)requestLocateWithModel:(UserDeviceUsedModel *)model {
    BMWeakSelf
    [HttpRequest returnLocateDeviceWithDeviceId:model.deviceid?:@"" success:^(id object) {
        Show_SV_Success_Msg(@"归还设备成功");
        weakSelf.model.is_locate = NO;
        [weakSelf czh_setGroup];
        [weakSelf.tableView reloadData];
        if (weakSelf.returnDeviceSuccessBlock) {
            weakSelf.returnDeviceSuccessBlock();
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(@"归还设备失败");
    }];
}

- (void)requestTbkWithModel:(UserDeviceUsedModel *)model {
    BMWeakSelf
    NSString *account = self.model.account?:@"";
    NSString *device = model.deviceid?:@"";
    Show_NoMsg
    [HttpRequest returnTbkDeviceWithDeviceId:device account:account success:^(id object) {
        Show_SV_Success_Msg(@"归还设备成功");
        if (weakSelf.tbkCount == 1) {
            weakSelf.model.is_tbk = NO;
        }
        [weakSelf czh_setGroup];
        [weakSelf.tableView reloadData];
        if (weakSelf.returnDeviceSuccessBlock) {
            weakSelf.returnDeviceSuccessBlock();
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(@"归还设备失败");
    }];
}

#pragma mark - 弹窗
- (void)showPopoverData {
    PopoverAction *title = [PopoverAction actionWithTitle:@"请选择归还设备:" handler:nil];
    title.rejectClick = YES;
    BMWeakSelf
    NSMutableArray *array = [NSMutableArray arrayWithObject:title];
    for (int i = 0; i < self.usedDeviceArray.count; ++i) {
        UserDeviceUsedModel *model = [self.usedDeviceArray objectAtIndexSafe:i];
        if ([model isKindOfClass:[UserDeviceUsedModel class]]) {
            PopoverAction *action = [PopoverAction actionWithTitle:model.device_name?:@"" handler:^(PopoverAction *action) {
                [weakSelf handlePopoverClick:action.index];
            }];
            action.index = i;
            [array addObject:action];
        }
    }
    self.tbkCount = array.count;
    PopoverView *popoverView = [PopoverView popoverView];
    popoverView.style = PopoverViewStyleDefault;
    popoverView.showShade = YES;
    popoverView.hideAfterTouchOutside = YES; // 点击外部时不允许隐藏
    [popoverView showInWindowWithFrame:CGRectZero withActions:array];
}

- (void)handlePopoverClick:(NSInteger)index {
    UserDeviceUsedModel *model = [self.usedDeviceArray objectAtIndexSafe:index];
    if ([model isKindOfClass:[UserDeviceUsedModel class]]) {
        if ([model.typecode isEqualToString:@"VOIP-A"]) {
            [self requestTbkWithModel:model];
        }
        else if ([model.typecode isEqualToString:@"GPS"]) {
            [self requestLocateWithModel:model];
        }
    }
}
@end
